<template>
  <el-dialog
    title="指标列表"
    :visible.sync="dialogVisible"
    :modal="true"
    :appendToBody="true"
    width="1100px"
    v-dialog-drag
    :close-on-click-modal="false"
    top="5vh"
    @close="handleClose()"
    @open="handleOpen()"
  >
    <div class="selected-content">
      <div v-for="item in selectionList" :key="item.id" class="item">
        <span>{{ item.indexConfigName }}</span>
        <span class="del-item-btn" @click="handleDelSelection(item)"
          ><i class="el-icon-close"></i
        ></span>
      </div>
    </div>
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
      :disabled="disabledSearch"
      label-position="left"
    >
      <organizationCascader
        class="inline"
        :clearData="{
          factory: true,
          workshop: true,
          line: true,
        }"
        :isAllList="true"
        :arrData="queryParams"
        @change="queryParamsChange"
      ></organizationCascader>

      <el-form-item prop="equipId" :inline="true">
        <el-select
          placeholder="请选择设备"
          v-model="queryParams.equipId"
          size="small"
          style="width: 100%;"
        >
          <el-option
            v-for="item in equipList"
            :key="item.equipId"
            :label="item.equipName"
            :value="item.equipId"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入关键字"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="search-btn">
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getList"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <TableSelect
      ref="table"
      :data="list"
      :selectionList.sync="selectionList"
      :selectableCallBack="selectableCallBack"
      rowKey="id"
      v-loading="tableLoading"
    >
      <el-table-column label="指标编码" prop="indexConfigCode">
      </el-table-column>
      <el-table-column
        label="指标名称"
        prop="indexConfigName"
      ></el-table-column>
      <el-table-column label="所属车间" prop="workshopName"> </el-table-column>
      <el-table-column label="所属产线" prop="lineName"> </el-table-column>
      <el-table-column label="所属设备" prop="equipName"> </el-table-column>
      <el-table-column label="关联工位" prop="stationNames"> </el-table-column>
    </TableSelect>
    <pagination
      :total="pager.total"
      :page.sync="pager.pageNum"
      :limit.sync="pager.pageSize"
      @pagination="getList"
    />
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="beSureSelect()">保 存</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getIndexConfigList } from "@/api/indexMgmt/configuration";
import { getEquipList } from "@/api/equipMgmt/equipment";
import organizationCascader from "@/views/components/organizationCascader";
import TableSelect from "@/components/TableSelect";
import { mapState } from "vuex";
export default {
  components: { TableSelect, organizationCascader },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    // 不可选回调，返回true或false，传参为row
    selectableCallBack: {
      type: Function,
    },
    // 是否多选
    multiple: {
      type: Boolean,
      default: false,
    },
    // 默认展示[{id:"",indexName:""}]
    defaultValues: {
      type: Array,
      default: () => {},
    },
    // 默认筛选条件
    defaultSearch: {
      type: Object,
      default: null,
    },
    // 是否禁用筛选条件
    disabledSearch: {
      type: Boolean,
      default: false,
    },
    isQueryAll:{
      type: Boolean,
      default: true,
    }
  },
  data() {
    return {
      /**
       * queryParams.metaIndexs的格式是null或以下对象，不出现在筛选框中，有则显示文字，没有则不显示
          metaIndexIds: this.form.defRelaDtosId,
          metaIndexNames: this.form.defRelaDtosName,
        }
       */
      queryParams: {
        factoryDeptCode: null,
        workshopId: null,
        workshopCode:null,
        lineId: null,
        equipId:null,
        searchValue: null,
        metaIndexs: null,
        disableFlag:"N",
        indexMetaDto:{
          indexType:"ED"
        }
      },
      list: [],
      equipList:[],
      tableLoading: false,
      pager: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      selectionList: [],
    };
  },
  computed: {
    dialogVisible: {
      set(val) {
        this.$emit("update:visible", val);
      },
      get() {
        return this.visible;
      },
    }
  },

  methods: {
    handleClose() {
      this.queryParams = {
        factoryDeptCode: null,
        workshopId: null,
        workshopCode:null,
        lineId: null,
        equipId:null,
        searchValue: null,
        disableFlag:"N",
        metaIndexs: null,
        indexMetaDto:{
          indexType:"ED"
        }
      };
      this.list = [];
      this.selectionList = [];
      this.tableLoading = false;
      this.pager.total = 0;
      this.pager.pageNum = 1;
    },

    async handleOpen() {
      // 默认选中项
      this.selectionList = this.defaultValues?.length ? [...this.defaultValues] : [];
      // 默认筛选条件
      if (this.defaultSearch) {
        this.queryParams = Object.assign(this.queryParams, this.defaultSearch);
      }
      this.getEquip();
      this.getList();
    },

    // 获取设备列表
    getEquip() {
      this.tableLoading = true;
      const { factoryDeptCode, workshopId, lineId,sortcode,searchValue } = this.queryParams;
      const params = {
        query: {
          searchValue,
          factoryDeptCode,
          workshopId,
          lineId,
          sortcode
        },
        pageNum:1,
        pageSize:500
      };
      getEquipList(params)
        .then((res) => {
          this.equipList = res.rows || [];
        })
        .finally(() => {
          this.tableLoading = false;
        });
    },

    // 获取指标列表
    getList() {
      this.tableLoading = true;
      const { pageNum, pageSize } = this.pager;
      const { factoryDeptCode, workshopId, lineId, metaIndexs, equipId,disableFlag, indexMetaDto, searchValue, } = this.queryParams;
      const params = {
        query: {
          factoryDeptCode,
          workshopId,
          lineId,
          metaIndexIds: metaIndexs?.metaIndexIds || null,
          equipId,
          disableFlag,
          indexMetaDto:{
            indexType:indexMetaDto?.indexType || null
          },
          searchValue
        },
        pageNum,
        pageSize,
      };
      getIndexConfigList(params)
        .then((res) => {
          this.list = res.rows || [];
          this.pager.total = res.total;
        })
        .finally(() => {
          this.tableLoading = false;
        });
    },
    queryParamsChange({ factoryDeptCode, workshopId, lineId }) {
      this.queryParams.factoryDeptCode = factoryDeptCode;
      this.queryParams.workshopId = workshopId;
      this.queryParams.lineId = lineId;
      this.getEquip()
    },
    //重置
    resetQuery() {
      this.resetForm("queryForm");
      if (!this.disabledSearch) {
        this.queryParams.factoryDeptCode = null;
        this.queryParams.workshopCode = null;
        this.queryParams.workshopId = null;
        this.queryParams.lineId = null;
        this.queryParams.equipId = null;
        this.queryParams.searchValue = null;
        this.queryParams.metaIndexs = null;
      }
      this.pager.pageNum = 1;
      this.getEquip()
      this.getList();
    },
    handleDelSelection(item) {
      if (!item) {
        return;
      }
      if (this.$refs.table) {
        this.$refs.table.handleDelSelection(item);
      }
    },
    beSureSelect() {
      const selection = this.selectionList;
      if (!this.multiple) {
        if (selection.length > 1) {
          this.$message({
            message: "最多选择一条数据!",
            type: "warning",
          });
          return;
        }
      }
      this.$emit("closeDialog", selection || []);
      this.dialogVisible = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.selected-content {
  width: 100%;
  height: 88px;
  padding: 10px;
  border: 1px solid #dfe6ec;
  margin-bottom: 20px;
  overflow-y: auto;
  .item {
    height: 28px;
    line-height: 28px;
    margin-right: 10px;
    margin-bottom: 10px;
    padding: 0 10px;
    font-size: 14px;
    display: inline-block;
    vertical-align: middle;
    background: rgb(234, 248, 254);
    color: #409eff;
    .del-item-btn {
      margin-left: 10px;
      cursor: pointer;
    }
  }
}
::v-deep {
  .el-dialog .el-dialog__body {
    padding-top: 10px;
  }
  .el-form-item {
    margin-bottom: 15px;
    &:not(.search-btn) {
      .el-form-item__content {
        width: 150px;
      }
    }
  }
  .inline {
    display: inline-block;
  }
  .meta-index {
    .el-form-item__content {
      width: 120px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}
</style>
